<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

  <style>
    .pages {
      display: flex;
    }

    .pages li {
      list-style: none;
      padding: 5px;
      margin: 2px;
    }

    .active {
      background: blue;
      color: white;
    }


    .mask {
      position: absolute;
      left: 0px;
      top: 0px;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .dialog {
      width: 300px;
      height: 300px;
      background: white;
    }

    .asc {
      display: inline-block;
      width: 10px;
      height: 10px;
      background: url('./mock/1.png');
      background-size: cover;
      transform: rotate(90deg);
    }

    .desc {
      display: inline-block;
      width: 10px;
      height: 10px;
      background: url('./mock/1.png');
      background-size: cover;
      transform: rotate(270deg);
    }
  </style>

</head>

<body>
  <div id="app">

    <div>

      <input type="text" v-model="txt">
      <button @click="handleSearch">查询</button>
      <button @click="handleAdd">添加</button>

    </div>
    <table>
      <tr>
        <td>公司名称</td>
        <td>
          <span>薪水</span>
          <span :class="{asc:isAsc,desc:!isAsc}" @click="sort()"></span>

        </td>
        <td>操作</td>
      </tr>
      <tr v-for="item in getDatalist">
        <td>{{item.companyName}}</td>
        <td>{{item.salary}}</td>
        <td>
          <button @click="edit(item)">编辑</button>
          <button @click="del(item._id)">删除</button>
        </td>
      </tr>

    </table>
    <ul class="pages">
      <li v-for="n in getPages" :class="{active:(n-1)===pageno}" @click="setPage(n)">
        {{n}}
      </li>
    </ul>

    <!-- 编辑开始 -->
    <edit-dialog v-if="showEdit"  >
      <template #title>
        编辑
      </template>
      <template #body>
        职位名称：<input type="text" v-model="currentPosition.companyName">
        <button @click="handleUpdateSubmit">提交</button>
        <button @click="$emit('close')">取消</button>
      </template>
    </edit-dialog>
    <!-- 编辑结束 -->


    <edit-dialog v-if="showAdd">
      <template #title>
        添加
      </template>
      <template #body>
        职位名称：
        <input ref="companyName" type="text">
        <button @click="handleAddSubmit">提交</button>
        <button @click="$emit('close')">取消</button>
      </template>
    </edit-dialog>

  </div>

  <script type="text/template" id="temp">
    <div class="mask">

      <div class="dialog">
        <h1>
          <slot name="title">标题</slot></h1>
          <slot name="body"></slot>
         
      </div>
    </div>
  </script>

  <script>

    Vue.component('edit-dialog', {
      props: ['position'],
      data() {
        return {
          item: {
            companyName: ""
          }
        }
      },
      template: `#temp`,
      mounted() {
        this.item = Object.assign({}, this.position)
        console.log(this.position)
      },
      methods: {
        submit() {
          this.$emit('updatesubmit', this.item)
        }
      }
    })

    var vm = new Vue({
      el: "#app",
      data: {
        datalist: [],
        //每页展示的数据条数
        pagesize: 10,
        //页码，第几页
        pageno: 0,
        //输入框输入的内容
        txt: '',
        //查询的关键字
        keyword: '',
        //是否显示编辑
        showEdit: false,
        //是否显示添加的对话框
        showAdd: false,
        currentPosition: {},
        //是否为正序
        isAsc: true
      },
      computed: {
        getPages() {
          let list = this.datalist.filter((item) => {
            return item.companyName.indexOf(this.keyword) > -1
          })

          return Math.ceil(list.length / this.pagesize)
        },
        getDatalist() {

          let tmp = this.datalist.sort((a, b) => {
            let salaryA = parseInt(a.salary.replace(/k/i, ''));
            let salaryB = parseInt(b.salary.replace(/k/i, ''));
            if (this.isAsc) {
              return salaryA - salaryB;
            } else {
              return salaryB - salaryA;
            }
          })

          let list = tmp.filter((item) => {
            return item.companyName.indexOf(this.keyword) > -1
          })



          let start = this.pageno * this.pagesize;
          let end = start + this.pagesize;
          return list.slice(start, end)
        }
      },
      created() {
        fetch('./mock/data.json').then((res) => res.json()).then((res) => {
          console.log(res)
          this.datalist = res.data;
        })
      },
      methods: {
        setPage(n) {
          this.pageno = n - 1;
        },
        handleSearch() {
          this.keyword = this.txt;
        },
        del(id) {
          this.datalist.find((item, index) => {
            if (item._id === id) {
              this.datalist.splice(index, 1)
              return true;
            }
          })
        },
        edit(item) {
          this.currentPosition = Object.assign({},item);
          this.showEdit = true;
        },
        sort() {
          this.isAsc = !this.isAsc;
        },
        handleAdd() {
          this.showAdd = true
        },
        handleAddSubmit() {
          let val = this.$refs.companyName.value;
          this.datalist.push({
            _id:new Date().getTime().toString(16),
            companyName:val,
            salary:Math.floor(Math.random()*10000) +''
          })
          this.showAdd = false;
        },
        handleUpdateSubmit(){
          this.datalist.find((item, index) => {
            if (item._id === this.currentPosition._id ) {
              this.datalist.splice(index, 1, this.currentPosition)
              return true;
            }
          })
          this.showEdit = false;
        }

      }
    })
  </script>
</body>

</html>